/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package knn;

import Javabeans.Customer;
import csvloader.CSVLoader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author Muhammed
 */
public class Main {
    private static CSVLoader csvloader;
    private ArrayList<Customer> trainingSet, testingSet;
    public static void main(String[] args) throws FileNotFoundException, IOException {
        String testData="C:\\Users\\Muhammed\\Documents\\NetBeansProjects\\KNN\\src\\data\\testProdSelection.csv";
        String trainData="C:\\Users\\Muhammed\\Documents\\NetBeansProjects\\KNN\\src\\data\\prodsel.csv";
        
        Main main = new Main();

        main.loadTrainingData(trainData);
        main.loadTestingData(testData);

        main.getNeighbours(3, 0);
    }
    public ArrayList<Customer> getTrainingSet(){
        return trainingSet;
    }
    public ArrayList<Customer> getTestingSet(){
        return testingSet;
    }
    public void loadTrainingData(String path) throws IOException{
        System.out.println("##############Training Set##############");
        trainingSet=loadDataSet(path, trainingSet);
    }
    public void loadTestingData(String path) throws IOException{
        System.out.println("##############Testing Set##############");
        testingSet=loadDataSet(path, testingSet);
    }
    private ArrayList<Customer> loadDataSet(String path, ArrayList<Customer> dataSet) throws IOException{
        csvloader = new CSVLoader(path);
        dataSet = csvloader.getDataSet();
        return dataSet;
    }
    public void calculateSimilarity(Customer newCustomer, ArrayList<Customer> trainingSet){
        Iterator<Customer> it = trainingSet.iterator();

        Customer.setNeighbour(newCustomer);

        while(it.hasNext()){
            Customer oldCustomer = it.next();
            oldCustomer.calculateDistanceFromNeighbour();
        }
    }
    public Customer[] getNeighbours(int numberOfNeighbours, int indexOfNewCustomer){
        Customer newCustomer = this.getTestingSet().get(indexOfNewCustomer);

        Iterator<Customer> it = trainingSet.iterator();
        ArrayList<Customer> neighbours = new ArrayList<Customer>();
        while(it.hasNext()){
            Customer customer = it.next();
            if(neighbours.size()!=numberOfNeighbours){
                neighbours.add(customer);
            }else{
                isNeighbour(newCustomer, neighbours);
            }
        }
        return null;
    }
    private boolean isNeighbour(Customer newNeighbour, ArrayList<Customer> neighbours){
        Iterator<Customer> it = neighbours.iterator();
        while(it.hasNext()){
            Customer neighbour = it.next();
            if(newNeighbour.getDistanceFromNeighbour()<neighbour.getDistanceFromNeighbour()){
                neighbours.remove(neighbour);
                neighbours.add(newNeighbour);
                return true;
            }
        }
        return false;
    }
}
